﻿using System;
using System.Data.Common;

using Centido.Core;

namespace Centido.Management.ConsoleManager
{
    /// <summary>
    ///   A console command that logs the specified user to the specified domain.
    /// </summary>
    internal class LoginCommand : PreLoginCommandBase
    {
        /// <summary>
        ///   Gets the console command name.
        /// </summary>
        public override string Name
        {
            get { return "login"; }
        }


        /// <summary>
        ///   Gets the console command description.
        /// </summary>
        public override string Description
        {
            get { return Messages.LoginCommandDescription; }
        }

        
        /// <summary>
        ///   Gets the console command full description.
        /// </summary>
        public override string FullDescription
        {
            get { return Messages.LoginCommandFullDescription; }
        }


        /// <summary>
        ///   Executes the console command.
        /// </summary>
        /// <param name="context">The console command execution context.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="context"/> is null.</exception>
        public override void Execute(CommandExecutionContext context)
        {
            base.Execute(context);

            if (!CheckCommandParameters(context)) return;

            var connectionString = PopulateConnectionString(context);

            if (string.IsNullOrEmpty(connectionString)) return;

            var domainConfig = new DomainConfiguration
            {
                DbConnectionString = connectionString,
                DbProviderName = context.GetParameterByName(ProviderNameParameter).FirstValue,
            };

            // The schema parameter
            if (context.ParameterValueIsNotEmpty(
                    SchemaParameter,
                    SchemaShortParameter))
                domainConfig.DbSchemaName = context.GetParameterValue(SchemaParameter, SchemaShortParameter);

            try
            {
                context.CurrentDomain = Domain.Build(domainConfig);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();

                return;
            }
            catch (DbException e)
            {
                // Displaying the error message
                Console.WriteLine(e.Message);
                Console.WriteLine();

                // Displaying the connection string
                Console.WriteLine(Messages.UsedConnectionStringHint,
                    domainConfig.DbConnectionString);
                return;
            }

            // Changing the current command set
            context.ManagementConsole.CurrentCommandSet = new LoggedInCommandSet();

            // Displaying the success message
            Console.WriteLine(Messages.CommandCompletedMessage);
        }


        /// <summary>
        ///   Checks the console command parameters.
        /// </summary>
        /// <param name="context">The console command execution context.</param>
        /// <returns>Returns true if everything is ok; otherwise, returns false.</returns>
        protected override bool CheckCommandParameters(CommandExecutionContext context)
        {
            var result = base.CheckCommandParameters(context);

            if (!result)
                Console.WriteLine(Messages.LoginCommandNotAllParametersSpecifiedMessage);

            return result;
        }
    }
}
